﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OurFAS.Data;

namespace OurFAS.MyDataManage
{
    public partial class FrmAssetQuery : Form
    {
        private string m_sDataSource = null;

        public FrmAssetQuery()
        {
            InitializeComponent();
            InitControls();
        }

        private void InitControls()
        {
            cmbDataSource.SelectedIndex = 0;

            //首先加载所有的数据项
            dgvQuery.DataSource = AssetsHelper.GetBaseAssets();
            InitDataGridTitle();
            InitComboBox();

            //隐藏提示信息
            lblInfo.Visible = false;
        }

        private void InitDataGridTitle()
        {
            int count = dgvQuery.ColumnCount;
            for (int i = 0; i < count; i++)
            {
                if (dgvQuery.Columns[i].HeaderText == "AssetCode")
                {
                    dgvQuery.Columns[i].HeaderText = "资产编号";
                }
                else if (dgvQuery.Columns[i].HeaderText == "AssetTagCode")
                {
                    dgvQuery.Columns[i].HeaderText = "标签号";
                }
                else if (dgvQuery.Columns[i].HeaderText == "AssetName")
                {
                    dgvQuery.Columns[i].HeaderText = "资产名称";
                }
                else if (dgvQuery.Columns[i].HeaderText == "AssetSpec")
                {
                    dgvQuery.Columns[i].HeaderText = "规格型号";
                }
                else if (dgvQuery.Columns[i].HeaderText == "TypeCodeL")
                {
                    dgvQuery.Columns[i].HeaderText = "大类编码";
                }
                else if (dgvQuery.Columns[i].HeaderText == "TypeCodeM")
                {
                    dgvQuery.Columns[i].HeaderText = "小类编码";
                }
                else if (dgvQuery.Columns[i].HeaderText == "UseFor")
                {
                    dgvQuery.Columns[i].HeaderText = "资产用途";
                }
                else if (dgvQuery.Columns[i].HeaderText == "InBillCode")
                {
                    dgvQuery.Columns[i].HeaderText = "入库单号";
                }
                else if (dgvQuery.Columns[i].HeaderText == "ProductionFirm")
                {
                    dgvQuery.Columns[i].HeaderText = "生产厂商";
                }
                else if (dgvQuery.Columns[i].HeaderText == "OriCost")
                {
                    dgvQuery.Columns[i].HeaderText = "资产原值";
                }
                else if (dgvQuery.Columns[i].HeaderText == "StartUseDate")
                {
                    dgvQuery.Columns[i].HeaderText = "启用日期";
                }
                else if (dgvQuery.Columns[i].HeaderText == "InCharge")
                {
                    dgvQuery.Columns[i].HeaderText = "负责人";
                }
                else if (dgvQuery.Columns[i].HeaderText == "Dept")
                {
                    dgvQuery.Columns[i].HeaderText = "所在部门";
                }
                else if (dgvQuery.Columns[i].HeaderText == "TaskCode")
                {
                    dgvQuery.Columns[i].HeaderText = "课题段代码";
                }
                else if (dgvQuery.Columns[i].HeaderText == "GetType")
                {
                    dgvQuery.Columns[i].HeaderText = "获取方式";
                }
                else if (dgvQuery.Columns[i].HeaderText == "PurchaseFrom")
                {
                    dgvQuery.Columns[i].HeaderText = "采购来源";
                }
                else if (dgvQuery.Columns[i].HeaderText == "GBCode")
                {
                    dgvQuery.Columns[i].HeaderText = "国标编号";
                }
                else if (dgvQuery.Columns[i].HeaderText == "MeasureInfo")
                {
                    dgvQuery.Columns[i].HeaderText = "计量信息";
                }
                else if (dgvQuery.Columns[i].HeaderText == "Location")
                {
                    dgvQuery.Columns[i].HeaderText = "具体位置";
                }
                else if (dgvQuery.Columns[i].HeaderText == "CertificateNo")
                {
                    dgvQuery.Columns[i].HeaderText = "合格证号";
                }
                else if (dgvQuery.Columns[i].HeaderText == "RepairDate")
                {
                    dgvQuery.Columns[i].HeaderText = "维护日期";
                }
            }
            dgvQuery.Refresh();
        }

        private void InitComboBox()
        {
            cmbAssetName.Items.Clear();
            cmbDept.Items.Clear();
            cmbGetType.Items.Clear();
            cmbMeasureInfo.Items.Clear();
            cmbUseFor.Items.Clear();

            //初始化“资产名称”下拉框
            foreach (Dict_Category record in AssetsHelper.GetCategorys())
            {
                cmbAssetName.Items.Add(record.Category.Trim());
            }

            //初始化“所在部门”下拉框
            foreach (Dict_Dept record in AssetsHelper.GetDepartments())
            {
                cmbDept.Items.Add(record.Dept.Trim());
            }

            //初始化“责任人”下拉框
            

            //初始化“获取方式”下拉框
            foreach (Dict_GetType record in AssetsHelper.GetGetTypes())
            {
                cmbGetType.Items.Add(record.GetType.Trim());
            }

            //初始化“计量信息”下拉框
            foreach (Dict_MeasureInfo record in AssetsHelper.GetMeasureInfos())
            {
                cmbMeasureInfo.Items.Add(record.MeasureInfo.Trim());
            }

            //初始化“资产用途”下拉框
            foreach (Dict_UseFor record in AssetsHelper.GetUseFors())
            {
                cmbUseFor.Items.Add(record.UseFor.Trim());
            }
        }

        private void chbEnterUseDate_CheckedChanged(object sender, EventArgs e)
        {
            dtpUseDate.Enabled = chbEnterUseDate.Checked;
            cmbUseDate.Enabled = chbEnterUseDate.Checked;

            if (chbEnterUseDate.Checked)
            {
                cmbUseDate.SelectedIndex = 0;
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            dgvQuery.DataSource = null;

            string dtUseDate = null;
            if (chbEnterUseDate.Checked)
            {
                dtUseDate = DataHelper.GetDateToString(dtpUseDate.Value);
            }

            if (m_sDataSource == "base")
            {
                IList<Tbl_BaseAssets> list = AssetsHelper.GetBaseAssetsByKeyWords(null, cmbAssetName.Text,
                    cmbDept.Text, cmbGetType.Text, cmbMeasureInfo.Text,
                    cmbUseFor.Text, dtUseDate, cmbUseDate.SelectedIndex);
                if (list.Count() < 1)
                {
                    lblInfo.Visible = true;
                    lblInfo.Text = "没有查询到相关记录！";
                }
                else
                {
                    lblInfo.Visible = false;
                    dgvQuery.DataSource = list;
                }
            }
            else if (m_sDataSource == "self")
            {
                IList<Tbl_SelfAssets> list = AssetsHelper.GetSelfAssetsByKeyWords(null, cmbAssetName.Text,
                    cmbDept.Text, cmbGetType.Text, cmbMeasureInfo.Text,
                    cmbUseFor.Text, dtUseDate, cmbUseDate.SelectedIndex);
                if (list.Count() < 1)
                {
                    lblInfo.Visible = true;
                    lblInfo.Text = "没有查询到相关记录！";
                }
                else
                {
                    lblInfo.Visible = false;
                    dgvQuery.DataSource = list;
                }
            }
            else if (m_sDataSource == "measure")
            {
                IList<Tbl_MeasureAssets> list = AssetsHelper.GetMeasureAssetsByKeyWords(null, cmbAssetName.Text,
                    cmbDept.Text, cmbGetType.Text, cmbMeasureInfo.Text,
                    cmbUseFor.Text, dtUseDate, cmbUseDate.SelectedIndex);
                if (list.Count() < 1)
                {
                    lblInfo.Visible = true;
                    lblInfo.Text = "没有查询到相关记录！";
                }
                else
                {
                    lblInfo.Visible = false;
                    dgvQuery.DataSource = list;
                }
            }
            InitDataGridTitle();
        }

        private void cmbDataSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbDataSource.SelectedIndex == 0)
            {
                m_sDataSource = "base";
            }
            else if (cmbDataSource.SelectedIndex == 1)
            {
                m_sDataSource = "self";
            }
            else if (cmbDataSource.SelectedIndex == 2)
            {
                m_sDataSource = "measure";
            }
        }

        private void btnOutput_Click(object sender, EventArgs e)
        {
            switch (m_sDataSource)
            {
                case "base":
                    {
                        IList<Tbl_BaseAssets> recordList = dgvQuery.DataSource as List<Tbl_BaseAssets>;
                        List<Dictionary<string, string>> dictList = new List<Dictionary<string, string>>();
                        foreach (Tbl_BaseAssets record in recordList)
                        {
                            Dictionary<string, string> reportDict = new Dictionary<string, string>();
                            reportDict.Add("AssetName", record.AssetName);
                            reportDict.Add("AssetTagCode", record.AssetTagCode);
                            reportDict.Add("AssetSpec", record.AssetSpec);
                            reportDict.Add("TypeCodeL", record.TypeCodeL);
                            reportDict.Add("TypeCodeM", record.TypeCodeM);
                            reportDict.Add("StartUseDate", record.StartUseDate);
                            reportDict.Add("UseFor", record.UseFor);
                            reportDict.Add("ProductionFirm", record.ProductionFirm);
                            reportDict.Add("OriCost", record.OriCost.ToString("F2"));
                            reportDict.Add("InCharge", record.InCharge);
                            reportDict.Add("Dept", record.Dept);
                            reportDict.Add("MeasureInfo", record.MeasureInfo);

                            dictList.Add(reportDict);
                        }
                        MyReport.ReportPrintForm window = new MyReport.ReportPrintForm(StringHelper.sReprotAssetList, dictList);
                        window.Show();
                    }
                    break;
                case  "self":
                    {
                        IList<Tbl_SelfAssets> recordList = dgvQuery.DataSource as List<Tbl_SelfAssets>;
                        List<Dictionary<string, string>> dictList = new List<Dictionary<string, string>>();
                        foreach (Tbl_SelfAssets record in recordList)
                        {
                            Dictionary<string, string> reportDict = new Dictionary<string, string>();
                            reportDict.Add("AssetName", record.AssetName);
                            reportDict.Add("AssetTagCode", record.AssetTagCode);
                            reportDict.Add("AssetSpec", record.AssetSpec);
                            reportDict.Add("TypeCodeL", record.TypeCodeL);
                            reportDict.Add("TypeCodeM", record.TypeCodeM);
                            reportDict.Add("StartUseDate", record.StartUseDate);
                            reportDict.Add("UseFor", record.UseFor);
                            reportDict.Add("ProductionFirm", record.ProductionFirm);
                            reportDict.Add("OriCost", record.OriCost.ToString("F2"));
                            reportDict.Add("InCharge", record.InCharge);
                            reportDict.Add("Dept", record.Dept);
                            reportDict.Add("MeasureInfo", record.MeasureInfo);

                            dictList.Add(reportDict);
                        }
                        MyReport.ReportPrintForm window = new MyReport.ReportPrintForm(StringHelper.sReprotAssetList, dictList);
                        window.Show();
                    }
                    break;
                case "measure":
                    {
                        IList<Tbl_MeasureAssets> recordList = dgvQuery.DataSource as List<Tbl_MeasureAssets>;
                        List<Dictionary<string, string>> dictList = new List<Dictionary<string, string>>();
                        foreach (Tbl_MeasureAssets record in recordList)
                        {
                            Dictionary<string, string> reportDict = new Dictionary<string, string>();
                            reportDict.Add("AssetName", record.AssetName);
                            reportDict.Add("AssetTagCode", record.AssetTagCode);
                            reportDict.Add("AssetSpec", record.AssetSpec);
                            reportDict.Add("TypeCodeL", record.TypeCodeL);
                            reportDict.Add("TypeCodeM", record.TypeCodeM);
                            reportDict.Add("StartUseDate", record.StartUseDate);
                            reportDict.Add("UseFor", record.UseFor);
                            reportDict.Add("ProductionFirm", record.ProductionFirm);
                            reportDict.Add("OriCost", record.OriCost.ToString("F2"));
                            reportDict.Add("InCharge", record.InCharge);
                            reportDict.Add("Dept", record.Dept);
                            reportDict.Add("MeasureInfo", record.MeasureInfo);

                            dictList.Add(reportDict);
                        }
                        MyReport.ReportPrintForm window = new MyReport.ReportPrintForm(StringHelper.sReprotAssetList, dictList);
                        window.Show();
                    }
                    break;

            }
            
        }
    }
}
